﻿

using QFramework;
using System.Linq;
using UnityEngine;

namespace ProjectChildhood
{
    /// <summary>
    /// 减少物体的命令类(命令设计模式)
    /// </summary>
    public class SubItemCountCommand : AbstractCommand
    {

        private readonly int m_SubCount;
        private readonly string m_ItemName;

        public SubItemCountCommand(string itemName, int subCount)
        {
            m_SubCount = subCount;
            m_ItemName = itemName;
        }

        protected override void OnExecute()
        {
            var system = this.GetSystem<IToolBarSystem>();
            ToolbarSlot slot = system.Slots.FirstOrDefault(slot => slot.ItemName == m_ItemName);
            if (slot != null)
            {
                slot.Count.Value -= m_SubCount;
                ToolBarSystem.OnItemCountChanged.Trigger(slot, slot.Count);
            }
        }
    }
}
